package com.mgm.utils;

import java.util.Random;


public class RandomCodeUtils {
	
	public static final String All_CHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	public static final String LETTER_CHAR = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	public static final String LOWER_CHAR = "abcdefghijklmnopqrstuvwxyz";
	public static final String UPPER_CHAR = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	public static final String NUM_CHAR = "0123456789";

	/**
	 * 
	 * @Title: generateString   
	 * @Description: 产生一个长度为指定的随机字符串的字符数，字符将从拉丁字母和阿拉伯数字中（0-9、a-z、A-Z的选择）
	 * @param: @param length
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	public static String random(int length) {
		StringBuffer sb = new StringBuffer();
		Random random = new Random();
		for (int i = 0; i < length; i++) {
			sb.append(All_CHAR.charAt(random.nextInt(All_CHAR.length())));
		}
		return sb.toString();
	}

	/**
	 * 
	 * @Title: randomAlphabetic   
	 * @Description: 产生一个长度为指定的随机字符串的字符数，字符将从拉丁字母（a-z、A-Z的选择）
	 * @param: @param length 长度
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	public static String randomAlphabetic(int length) {
		StringBuffer sb = new StringBuffer();
		Random random = new Random();
		for (int i = 0; i < length; i++) {
			sb.append(LETTER_CHAR.charAt(random.nextInt(LETTER_CHAR.length())));
		}
		return sb.toString();
	}

	/**
	 * 
	 * @Title: randomLowerAlphabetic   
	 * @Description: 产生一个长度为指定的随机(小写字母)字符串（a-z）
	 * @param: @param length
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	public static String randomLowerAlphabetic(int length) {
		return randomAlphabetic(length).toLowerCase();
	}

	/**
	 * 
	 * @Title: randomUpperAlphabetic   
	 * @Description: 产生一个长度为指定的随机(大写字母)字符串（A-Z） 
	 * @param: @param length
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	public static String randomUpperAlphabetic(int length) {
		return randomAlphabetic(length).toUpperCase();
	}

	/**
	 * 
	 * @Title: generateZeroString   
	 * @Description: 生成一个定长的纯0字符串
	 * @param: @param length
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	public static String generateZero(int length) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			sb.append('0');
		}
		return sb.toString();
	}


	/**
	 * 
	 * @Title: toFixdLengthString   
	 * @Description: 根据数字生成一个定长的字符串，长度不够前面补0
	 * @param: @param num
	 * @param: @param fixdlenth
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	public static String toFixdLengthString(int num, int fixdlenth) {
		StringBuffer sb = new StringBuffer();
		String strNum = String.valueOf(num);
		if (fixdlenth - strNum.length() >= 0) {
			sb.append(generateZero(fixdlenth - strNum.length()));
		} else {
			throw new RuntimeException("将数字" + num + "转化为长度为" + fixdlenth + "的字符串发生异常！");
		}
		sb.append(strNum);
		return sb.toString();
	}
	
	/**
	 * 
	 * @Title: randomNumeric   
	 * @Description: 创建一个随机字符串，其长度是指定的字符数，将从数字字符集中选择字符。 
	 * @param: @param length
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	public static String randomNumeric(int length) {
		StringBuilder sb = new StringBuilder();
		Random random = new Random();
		for (int i = 0; i < length; i++) {
			sb.append(random.nextInt(NUM_CHAR.length()));
		}
		return sb.toString();
	}
	
	/**
	 * 
	 * @Title: radomIntNumeric   
	 * @Description: 创建一个指定范围内的整数值   
	 * @param: @param num
	 * @param: @return      
	 * @return: int      
	 * @throws
	 */
	public static int radomIntNumeric(int num) {
		Random rand = new Random();
		int n = rand.nextInt(num);
		return n;
	}
	
	public static void main(String[] args) {
		System.out.println(random(15)); 
		System.out.println(randomAlphabetic(15)); 
		System.out.println(randomLowerAlphabetic(15)); 
		System.out.println(randomUpperAlphabetic(15)); 
		System.out.println(generateZero(15)); 
		System.out.println(toFixdLengthString(123, 15)); 
		System.out.println(randomNumeric(4));
		System.out.println("============");
		for (int i = 0; i < 1000; i++) {
			System.out.println(radomIntNumeric(10000));
		}
		
	}

}
